package com.demo.datastructure.array;

import java.util.Arrays;

/**
 * 数组实现
 */
public class MyArray {
    /**
     * 数组大小
     */
    private int size;
    /**
     * 数组容量
     */
    private int capacity;
    /**
     * 扩容因子
     */
    private final static double FACTOR = 0.5D;
    private String[] iterms;

    public int getSize() {
        return size;
    }

    public MyArray() {
        this.size = 0;
        this.capacity = 16;

        iterms = new String[capacity];
    }

    public MyArray(int capacity) {
        this.capacity = capacity;
        iterms = new String[capacity];
        size = 0;
    }

    /**
     * 添加一个数据，没有容量后扩容
     *
     * @return
     */
    public void add(String element) {
        if (size == capacity) {
            String[] tmp = increaseCapacity();
            tmp[size] = element;
            iterms = tmp;
        }
        iterms[size] = element;
        ++size;
    }
    public String[] increaseCapacity()
    {
        capacity = (int) (capacity + capacity * FACTOR);
        String[] tmp = new String[capacity];

        for (int i = 0; i < size; i++) {
            tmp[i] = iterms[i];
        }
        return tmp;
    }
    /**
     * 在某个下标添加一个元素 尾部插入
     * @param element
     * @param index
     */
    public void add(String element,int index)
    {
        if (index < 0 || index >= size || size==capacity)
        {
            System.out.println("error index is out of range");
            return;
        }

        for (int i = size; i > index;i--)
        {
            iterms[i] = iterms[i-1];
        }
        iterms[index] = element;
        size++;

    }
    /**
     * 移除一个数据
     *
     * @param element
     * @return
     */
    public boolean remove(String element) {
        if (size==0)
        {
            return false;
        }

        for (int i = 0; i < size;i++)
        {
            if (iterms[i].equals(element))
            {
                for (int j = i+1;j < size;j++)
                {
                    iterms[j-1] = iterms[j];
                    iterms[j] = null;
                }
                size--;
                return true;
            }
        }
        return false;
    }

    /**
     * 更新某个index的数据
     *
     * @param index
     * @param element
     * @return
     */
    public boolean update(int index,String element) {
        if (size == 0 || index >= size || index < 0)
        {
            return false;
        }

        iterms[index] = element;

        return true;
    }

    /**
     * 获取某个index的值
     *
     * @param index
     * @return
     */
    public String get(int index) {
        if (size == 0 || index >= size || index < 0)
        {
            return null;
        }

        return iterms[index];
    }

    /**
     * 反转
     *
     * @return
     */
    public String[] reversal() {
        if (size == 0)
        {
            return null;
        }
        String[] tmp = new String[capacity];
        int j = size;
        for (int i = 0;i< size;i++)
        {
            tmp[i] = iterms[--j];
        }
        iterms = tmp;
        return iterms;
    }
}
